package com.yhgm.service.impl;

import com.yhgm.constant.Constant;
import com.yhgm.mapper.TimerDao;
import com.yhgm.model.entity.Timer;
import com.yhgm.service.TimerService;
import com.yhgm.utils.R;
import com.yhgm.utils.timer.QuartzUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class TimerServiceImpl implements TimerService {
    @Resource
    private TimerDao timerDao;
    @Resource
    private QuartzUtil quartzUtil;

    @Override
    public R addTimer(Timer timer) {
        if (timer.getTimerId().equals(Constant.BACKUPS_JOB_KEY)) {
            if (timerDao.isJobExist(timer.getTimerId()) > 0) {
                return R.error("该任务已存在，请勿重复创建");
            }
        }else if (timer.getTimerId().equals(Constant.FILE_DELETE_JOB_KEY)) {
            if (timerDao.isJobExist(timer.getTimerId()) > 0) {
                return R.error("该任务已存在，请勿重复创建");
            }
        } else {
            return R.error("禁止创建非法任务");
        }
        Boolean add = quartzUtil.add(timer.getTimerId(), timer.getCorn());
        if (add) {
            timer.setCreationTime(new Date(System.currentTimeMillis()));
            timerDao.addOne(timer);
            return R.ok("创建定时任务成功");
        } else {
            return R.error("创建定时任务失败");
        }
    }

    @Override
    public R updateTimer(Timer timer) {
        Timer oneById = timerDao.getOneById(timer.getId());
        //if (oneById.getTimerId().equals(timer.getTimerId())){
        if (!oneById.getCorn().equals(timer.getCorn())) {
            if (!quartzUtil.update(oneById.getTimerId(), timer.getCorn())) {
                return R.error("更新定时任务Corn失败");
            }
        }
        //}else {
        //quartzUtil.del(timer.getTimerId());
        //to do：可以改变任务类型
        //return R.error("禁止改变任务类型");
        //}
        timerDao.updateOneById(timer);
        return R.ok("更新定时任务成功");
    }

    @Override
    public R deleteTimer(Long id) {
        String timerIdById = timerDao.getTimerIdById(id);
        if (quartzUtil.del(timerIdById)) {
            timerDao.deleteOneById(id);
            return R.ok("删除定时任务成功");
        } else {
            return R.error("删除定时任务失败");
        }
    }

    @Override
    public R pauseTimer(Long id) {
        String timerIdById = timerDao.getTimerIdById(id);
        if (quartzUtil.pause(timerIdById)) {
            timerDao.updateStateById(0, id);
            return R.ok("暂停定时任务成功");
        } else {
            return R.error("暂停定时任务失败");
        }
    }

    @Override
    public R resumeTimer(Long id) {
        String timerIdById = timerDao.getTimerIdById(id);
        if (quartzUtil.resume(timerIdById)) {
            timerDao.updateStateById(1, id);
            return R.ok("重启定时任务成功");
        } else {
            return R.error("重启定时任务失败");
        }
    }

    @Override
    public R multipleAndFuzzy(Timer timer, int page, int size) {
        if (page < 1) {
            return R.error("page couldn't less than 1");
        } else {
            page = (page - 1) * size;
            Map<String, Object> data = new HashMap<>();
            data.put("list", timerDao.multipleAndFuzzy(timer, page, size));
            data.put("total", timerDao.countMultipleAndFuzzy(timer));
            return R.ok(data, "multipleAndFuzzy");
        }
    }
}
